﻿using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 定时器管理器
/// </summary>
public class UpdateManager : MonoBehaviour
{
    private static UpdateManager instance;
    public static UpdateManager Instance
    {
        get
        {
            if (instance == null)
            {
                Type type = typeof(UpdateManager);
                GameObject go = new GameObject(type.Name, type);
                instance = go.GetComponent<UpdateManager>();
                go.hideFlags = HideFlags.HideInHierarchy;
            }
            return instance;
        }
    }
    private void Update()
    {
        for (int i = timers.Count - 1; i >= 0; i--)
        {
            var timer = timers[i];
            timer?.Invoke(Time.deltaTime);
        }
    }

    private List<Action<float>> timers = new List<Action<float>>();

    public void AddUpdateTimer(Action<float> timer)
    {
        timers.Add(timer);
    }

    public void RemoveUpdateTimer(Action<float> timer)
    {
        timers.Remove(timer);
    }
}